#include "Other/PlantsVsZombies.h"

int oneTwice = 0;

PlantVsZombies::PlantVsZombies() {
	this->init_data(); // 初始化数据
 // 开启调度函数 
	//this->create_sun(500, 500);

	this->scheduleUpdate();
} // 构造函数

void PlantVsZombies::init_data() {
	time1 = 14;
	time2 = 40;
	time3 = 70;
	time4 = 70;
	time5 = 90;
	time6 = 110;
	time7 = 120;
	time8 = 150;
	// 判断是否经过了这个时间段了
	is_pass_time1 = false;
	is_pass_time2 = false;
	is_pass_time3 = false;
	is_pass_time4 = false;
	is_pass_time5 = false;
	is_pass_time6 = false;
	is_pass_time7 = false;
	is_pass_time8 = false;
	time_count = 0.0;//用于计时，到达指定时间，生成对应波次的僵尸
	pruduce_time = 0.0; // 生成僵尸的时间间隔  比如能被一个数整除就生成  time_count % produce == 0 

	is_game_fail = false;//判断游戏失败
	is_game_success = false;//判断游戏是否获得胜利

	//this->scheduleUpdate();
}//用于初始化 数据 

void PlantVsZombies::update(float update_time) {
	this->pruduce_time += update_time;
	this->time_count += update_time;
	// 第n波进攻
	int rand_row  = 0;
	if (this->time_count >= time1 && !this->is_pass_time1) {
		//生成僵尸
			create_zombie(2, ZombieType::zombie_type_flag);
		this->is_pass_time1 = true;
	}

	if (this->time_count >= time2 && !this->is_pass_time2) {
		//生成僵尸
		create_zombie(3, ZombieType::zombie_type_conehead);
		create_zombie(2, ZombieType::zombie_type_normal);
		create_zombie(1, ZombieType::zombie_type_normal);
		this->is_pass_time2 = true;
	}

	if (this->time_count >= time3 && !this->is_pass_time3) {
		create_zombie(4, ZombieType::zombie_type_conehead);
		create_zombie(3, ZombieType::zombie_type_normal);
		create_zombie(2, ZombieType::zombie_type_buckethead);
		create_zombie(1, ZombieType::zombie_type_normal);
		create_zombie(0, ZombieType::zombie_type_normal);
		this->is_pass_time3 = true;
	}

	//if (this->time_count >= time4 && !this->is_pass_time4) {
	//	//生成僵尸
	//	create_zombie(5, ZombieType::zombie_type_normal);
	//	create_zombie(4, ZombieType::zombie_type_normal);
	//	create_zombie(3, ZombieType::zombie_type_flag);
	//	create_zombie(2, ZombieType::zombie_type_buckethead);
	//	create_zombie(1, ZombieType::zombie_type_conehead);
	//	this->is_pass_time4 = true;
	//}

	//if (this->time_count >= time5 && !this->is_pass_time5) {
	//	//生成僵尸
	//	create_zombie(5, ZombieType::zombie_type_normal);
	//	create_zombie(4, ZombieType::zombie_type_normal);
	//	create_zombie(2, ZombieType::zombie_type_buckethead);
	//	create_zombie(1, ZombieType::zombie_type_conehead);
	//	this->is_pass_time5 = true;
	//}

	//if (this->time_count >= time6 && !this->is_pass_time6) {
	//	//生成僵尸
	//	create_zombie(5, ZombieType::zombie_type_normal);
	//	create_zombie(4, ZombieType::zombie_type_normal);
	//	create_zombie(2, ZombieType::zombie_type_buckethead);
	//	create_zombie(1, ZombieType::zombie_type_conehead);
	//	this->is_pass_time6 = true;
	//}

	//if (this->time_count >= time7 && !this->is_pass_time7) {
	//	//生成僵尸
	//	create_zombie(2, ZombieType::zombie_type_normal);
	//	create_zombie(1, ZombieType::zombie_type_normal);
	//	create_zombie(4, ZombieType::zombie_type_buckethead);
	//	create_zombie(5, ZombieType::zombie_type_conehead);
	//	this->is_pass_time7 = true;
	//}

	//if (this->time_count >= time8 && !this->is_pass_time8) {
	//	//生成僵尸
	//	create_zombie(3, ZombieType::zombie_type_flag);
	//	create_zombie(5, ZombieType::zombie_type_conehead);
	//	create_zombie(4, ZombieType::zombie_type_buckethead);
	//	create_zombie(2, ZombieType::zombie_type_buckethead);
	//	create_zombie(1, ZombieType::zombie_type_conehead);
	//	this->is_pass_time8 = true;
	//}
	//遍历死亡
	for (auto& it : zombie_date) {
		if (it->is_dead) continue;
		if (it->blood <= 0) {
			it->run_zombie_die_animation();
			//it->sprite->setPosition(10000, 0);
			it->is_dead = true;
			it->sprite = nullptr;
			//it->sprite->runAction(RemoveSelf::create());
			continue;
		}
		for (auto& ca : car_date) {  // 当车子移动的时候遍历僵尸
			int row_ = ca->row;
			if (fabs(it->get_x() - ca->get_x()) <= 15 && ca->is_used && row_ == it->row) {
				//ca->is_used = true;
				it->blood = 0;
			}
		}
	}

	//遍历所有僵尸
	for (auto& it : zombie_date) {
		it->time_count += update_time;
		if (it->is_dead) {
			continue; // 如果僵尸死了
		}
		Plant* temp_plant = get_row_right_plant(it->row);
		if (temp_plant != nullptr) {
			if (it->time_count >= it->attack_time && fabs(it->get_x() - temp_plant->get_x()) <= 15) {
				if (!it->is_eat) it->run_zombie_eat_animation();
				temp_plant->by_attack(it->attack_num);
				it->time_count = 0.0;
			}
			else {
				if ((!it->judge_move() && fabs(it->get_x() - temp_plant->get_x()) >= 15 ) ) {  
					if(it->judge_eat())it->run_zombie_move_animation();
				}
			}
		}
		else if (it->judge_eat() ) {
				it->run_zombie_move_animation();
			}
	}

	for (auto it = plant_date.begin(); it != plant_date.end();) {
		//遍历植物  执行植物的相应攻击动作
		// 太阳花 就产生 太阳   豌豆就 生成子弹  
		//结合switch
		if ((*it)->get_blood() <= 0)
		{
			plant_judge_position[(*it)->get_row()][(*it)->get_line()] = false;
			(*it)->get_sprite()->removeFromParent();
			(*it)->removeFromParent();
			it = plant_date.erase(it);
			continue;
		}
		(*it)->set_is_shoot(update_time);
		if ((*it)->get_is_shoot() && (get_row_left_zombie((*it)->get_row()) != NULL || (*it)->get_type() == PlantType::SunFlower))
		{
			switch ((*it)->get_type())
			{
			case PlantType::SunFlower:
			{
				(*it)->time_count += update_time;
				if ((*it)->time_count >= (*it)->rate_time) {
					(*it)->time_count = 0.0;
					++oneTwice;
					if (oneTwice == 2) {
						oneTwice = 0;
						this->create_sun((*it)->get_x() + 30, (*it)->get_y() + 30);
					}
				}
				break;
			}
			case PlantType::Peashooter:
				this->peashooter_shoot((*it));
				break;
			default:
				break;
			}
		}
		it++;
	}


	for (auto& it : bullet_date) {
		//遍历所有子弹  如果碰撞了 就扣血 + 设置子弹为is_crash
		if (it->judge_crash() || get_row_left_zombie(it->row) == nullptr) continue;
		if (get_row_left_zombie(it->row)->get_x() - it->sprite->getPositionX() <= 0) {
			get_row_left_zombie(it->row)->blood -= it->attack_num;
			it->set_crash();

			// Add crash explosion animation and music
			//auto explode_delay = DelayTime::create(2.0f); // crash explosion delay
			auto explode_sprite = Sprite::create("Bullet/PeaNormalExplode/PeaNormalExplode_0.png");
			explode_sprite->setPosition(Vec2(get_row_left_zombie(it->row)->get_x(), get_row_left_zombie(it->row)->get_y()));
			this->addChild(explode_sprite, 3);
			AudioEngine::play2d("Music/ignite.ogg", false);
			//auto explode_movement = MoveTo::create(1.0f, Vec2(get_row_left_zombie(it->row)->get_x(), get_row_left_zombie(it->row)->get_y()));
			auto explode_vanish = FadeOut::create(1.0f);
			//auto crash_seq = Sequence::create(callback_explosion, explode_vanish, nullptr);
			//explode_sprite->runAction(crash_seq);
			explode_sprite->runAction(explode_vanish);
			//this->removeChild(explode_sprite); // remove explosion 

			this->removeChild(it->sprite); // remove bullets
		}
	}

	for (auto& it : car_date) {
		int row_ = it->row;
		Zombie* temp_zombie = get_row_left_zombie(row_);
		if (it->is_used || temp_zombie == nullptr) continue;
		if (fabs(temp_zombie->get_x() - it->get_x()) <= 15) {
			it->init_car_move_animation();
			//it->is_used = true;
			temp_zombie->blood = 0;
		} 
	}

	//执行生成阳光 根据间隔时间生成
	if (pruduce_time >= 15.0) {
		pruduce_time = 0.0;//置零
		int rand_x = rand() % 500 + 100;
		int rand_y = rand() % 600 + 200;
		this->create_sun(rand_x, rand_y);
	}

	//阳光超过指定时间没有收集就删除
	for (auto& it : sun_date) {
		if (it->judge_delete()) continue;
		if (it->sun_time >= 8.0) {
			it->set_is_delete();
			it->run_sun_delete_animation();
		}
		it->sun_time += update_time;
	}

	int zombie_cnt = 0;
	//如果游戏结束了 
	for (auto& it : zombie_date) {
		int x = it->row;
		if (it->judge_dead()) continue; //如果状态为dead
		zombie_cnt++;
		if (it->sprite != nullptr && it->get_x() <= 0 && car_date[x] -> is_used ) {
			is_game_fail = true;
			break;
		}
	}

	if (this->is_pass_time3 == true && zombie_cnt == 0) {
		//如果最后一只僵尸死亡  游戏胜利
		init_data();
		is_game_success = true;
	}
}

void PlantVsZombies::init_scene(Scene* scene_) {
	scene = scene_;
}//用于绑定场景，addChild     游戏场景：new一个节点PvsZ  然后 this->addchild(本节点)

Zombie* PlantVsZombies::get_row_left_zombie(int row_) {
	Zombie* left_zombie = nullptr;
	double left = -1;
	for (auto& it : zombie_date) {
		if (it->row != row_ || it->is_dead) continue;
		if (left == -1 || left >= it->get_x()) {
			left = it->get_x();
			left_zombie = it;
		}
	}
	return left_zombie;
}//获取在第row路最左边的僵尸  


Plant* PlantVsZombies::get_row_right_plant(int row_) {
	Plant* right_plant = nullptr;
	double right = -1.0;
	for (auto& it : plant_date) {
		if (it->row != row_ || it->get_blood() <= 0) continue;
		if (right == -1 || right <= it->get_x()) {
			right = it->get_x();
			right_plant = it;
		}
	}
	return right_plant;
}//获取在第row行最右边的植物

void PlantVsZombies::peashooter_shoot(Plant* plant_) {
	Bullet* temp_bullet = new Bullet;
	//temp_bullet->sprite_init(plant_->get_position());
	temp_bullet->sprite_init(Vec2(plant_->get_x() + 30, plant_->get_y() + 20)); // The bullet should come out of the muzzle
	temp_bullet->bullet_move_animation();
	this->addChild(temp_bullet->sprite, 2);
	temp_bullet->row = plant_->row;
	bullet_date.push_back(temp_bullet);
	plant_->set_is_shoot(false);

	AudioEngine::play2d("Music/firepea.ogg", false); // firing music
}//豌豆射手发射一个豌豆


void PlantVsZombies::create_zombie(int row_, ZombieType type_) {
	Zombie* temp_zombie;
	switch (type_)
	{
	case zombie_type_normal:
	{
		temp_zombie = new NormalZombie();
		break;
	}
	case zombie_type_conehead:
	{
		temp_zombie = new ConeheadZombie();
		break;
	}
	case zombie_type_buckethead:
	{
		temp_zombie = new BucketheadZombie();
		break;
	}
	case zombie_type_flag:
	{
		temp_zombie = new FlagZombie();
		break;
	}
	default:
		break;
	}
	temp_zombie->row = row_;  // 0 1 2 3 4
	temp_zombie->sprite_init(zombie_positon[row_]);
	temp_zombie->run_zombie_move_animation();
	this->addChild(temp_zombie->sprite, 3);
	zombie_date.push_back(temp_zombie);
}//在row行创建一个某种类型的僵尸

void PlantVsZombies::create_plant(int row_, int col_, PlantType type_) {
	plant_judge_position[row_][col_] = true;
}//在row行col列创建一个某种类型的植物

bool PlantVsZombies::judge_plant(int row_, int col_) {
	return plant_judge_position[row_][col_];
}//用于判断在row行col列是否可以种植植物  需要


//判断这个vec2坐标 鼠标点击的位置是不是在草地区域  结合鼠标使用种植植物 
bool PlantVsZombies::judge_vec2_in_row_col(Vec2 vec_position, int& _row, int& _col) {
	//特判  ：越界 if(越界) return false
	auto visibleSize = Director::getInstance()->getVisibleSize();
	if ((vec_position.x < visibleSize.width * 0.18) || (vec_position.x > visibleSize.width * 0.95)
		|| (vec_position.y < visibleSize.height * 0.13) || (vec_position.y > visibleSize.height * 0.92))	return false;
	int width_ = 125 / 2;
	int height_ = 103 / 2;
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 9; j++) {
			Vec2 offset = vec_position - plant_position[i][j];
			if ((fabs(offset.x) <= width_) && (fabs(offset.y) <= height_)) {
				_row = i;
				_col = j;
				return true;
			}
		}
	}
	return false;
}

void PlantVsZombies::create_sun(int x_, int y_) {
	Sun* temp_sun = new Sun;
	temp_sun->sprite->setScale(1.5);
	this->addChild(temp_sun->sprite, 5); //添加层数
	sun_date.push_back(temp_sun);
	temp_sun->sprite->setPosition(Vec2(x_ + 30, y_ + 30));
	temp_sun->run_sun_produce_animation();
}//生成太阳 在 x y -》写到Vec里面
